package com.ruoyi.mes.controller;

import com.ruoyi.mes.domain.DeviceStatus;
import com.ruoyi.mes.utils.MqttClientUtil;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/devices")
public class DevicesController {

    private final MqttClientController mqttClientController;
    private final MqttClientUtil mqttClientUtil;

    @Autowired
    public DevicesController(MqttClientController mqttClientController, MqttClientUtil mqttClientUtil) {
        this.mqttClientController = mqttClientController;
        this.mqttClientUtil = mqttClientUtil;
    }
     @PostMapping("/controller")
     public DeviceStatus controller(@RequestParam String device, @RequestParam String command) throws MqttException {
         // 发送设备命令
         mqttClientController.Publish(device, command);
         DeviceStatus deviceStatus = new DeviceStatus();

         if(deviceStatus.getStatus() == null){

             mqttClientController.Subscribe(device);;
             return mqttClientUtil.getStatus(device);
         }
         else {
             return deviceStatus;
         }


    }











//     @PostMapping("/sixArm")
//     public DeviceStatus sixArm(@RequestParam String device, @RequestParam String command) throws MqttException {
//        // 调用Publish接口
//         mqttClientController.Publish(device, command);
//         mqttClientController.Subscribe();
//         return mqttClientController.getDeviceStatus(device);
//    }
//
//    @PostMapping("/fourArm")
//    public DeviceStatus fourArm(@RequestParam String device, @RequestParam String command) throws MqttException {
//        // 调用Publish接口
//        mqttClientController.Publish(device, command);
//        return mqttClientController.getDeviceStatus(device);
//    }
//
//    @PostMapping("/inspection")
//    public DeviceStatus inspection(@RequestParam String device, @RequestParam String command) throws MqttException {
//        // 调用Publish接口
//        mqttClientController.Publish(device, command);
//        return mqttClientController.getDeviceStatus(device);
//    }
//
//    @PostMapping("/marking")
//    public DeviceStatus marking(@RequestParam String device, @RequestParam String command) throws MqttException {
//        // 调用Publish接口
//        mqttClientController.Publish(device, command);
//        return mqttClientController.getDeviceStatus(device);
//    }
//
//    @PostMapping("/threeArm")
//    public DeviceStatus threeArm(@RequestParam String device, @RequestParam String command) throws MqttException {
//        // 调用Publish接口
//        mqttClientController.Publish(device, command);
//        return mqttClientController.getDeviceStatus(device);
//    }






    /**
     * 获取设备状态
     */

}
